package com.sidneysimmons.financially.fragments;

import java.io.Serializable;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.sidneysimmons.financially.Category;
import com.sidneysimmons.financially.R;

public class CategoryActionsFragment extends DialogFragment {
	
	private final String CLASS_NAME = this.getClass().getName();
	
	/** Used by the FragmentManager to identify this fragment. */
	public static String TAG = "category_actions_fragment";
	
	private Activity context;
	private Category category;
	
	public static CategoryActionsFragment newInstance(Category category) {
		Bundle bundle = new Bundle();
        bundle.putSerializable("category", category);
        
		CategoryActionsFragment fragment = new CategoryActionsFragment();
        fragment.setArguments(bundle);
		fragment.setCategory(category);

		return fragment;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// If the bundle is not null and category is null, restore the category
		// If the bundle is null, the category should already be retained in this object
		if (savedInstanceState != null && category == null) {
			Serializable serialCategory = savedInstanceState.getSerializable("category");
			if (serialCategory != null) {
				category = (Category) serialCategory;
			}
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setItems(R.array.category_actions, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				FragmentManager fragmentManager = null;
				try {
					fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
				} catch (ClassCastException e) {
					Log.e(CLASS_NAME, "Exception thrown while casting 'context' in CategoryActionsFragment.java to FragmentActivity.");
					Log.e(CLASS_NAME, "Not executing any Category Action.");
					return;
				}
				
	    		// Split off here - depending on which action was clicked
	    		if (which == 0) {
	    			
	    		} else if (which == 1) {
	    			
	    		} else if (which == 2) {
	    			EditCategoryDialogFragment editCategoryDialogFragment = EditCategoryDialogFragment.newInstance(category);
	    			editCategoryDialogFragment.show(fragmentManager, EditCategoryDialogFragment.TAG);
	    		}
	    	}
	    });
	    
	    return builder.create();
	}
	
	/**
	 * Called when the Fragment and it's Activity are attached.
	 */
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // Set this as the context to use later
        context = activity;
    }
	
	/**
	 * Use this to save any data right before the Fragment is destroyed.
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
		outState.putSerializable("category", category);
	}
	
	/**
	 * Set the Category object.
	 */
	public void setCategory(Category category) {
		this.category = category;
	}
	
	/**
	 * Gets the Category object.
	 * 
	 * @return Category
	 */
	public Category getCategory() {
		return category;
	}
}